package com.sangmo.fts.api.receivable;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.inventory.interfaces.IfLoanAuditRequest;
import com.sangmo.fts.process.receivable.interfaces.RpfLoanAuditRequest;
import com.sangmo.fts.process.receivable.interfaces.RpfLoanCreation;
import com.sangmo.fts.process.receivable.model.RpfContract;
import com.sangmo.fts.process.receivable.model.RpfLoan;
import com.sangmo.fts.process.receivable.service.RpfLoanService;
import com.sangmo.fts.process.storage.interfaces.CsfLoanCreation;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/rpfLoan/v1")
@Access("rpfLoan")
public class RpfLoanApi {

    @Autowired
    private RpfLoanService rpfLoanService;

    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_LOAN_SELLER_APPLY_5)
    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<RpfLoan> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid RpfLoanCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(rpfLoanService.create(obj));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<RpfLoan>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("RpfLoan.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(rpfLoanService.search(ps));
    }

    @Access(":read")
    @PostMapping(value = "/{trainingId}/{id}/read")
    public @ResponseBody
    ApiResponse<RpfLoan> read(@PathVariable("trainingId") Long trainingId, @PathVariable("id")Long id) {
        ExtViewHelper.setGroup("RpfLoan.detail", 1);
        return ApiResponse.make(rpfLoanService.find(id));
    }

    /** 受理 **/
    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_LOAN_BANK_ACCEPT_6)
    @Access(":accept")
    @PostMapping(value = "/{trainingId}/{id}/accept")
    public @ResponseBody
    ApiResponse accept(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        rpfLoanService.accept(id);
        return ApiResponse.make();
    }

    /** 审核 **/
    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_LOAN_BANK_AUDIT_11)
    @Access(":audit")
    @PostMapping(value = "/{trainingId}/{id}/audit")
    public @ResponseBody
    ApiResponse audit(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid RpfLoanAuditRequest request) {
        rpfLoanService.audit(id, request);
        return ApiResponse.make();
    }

    /** 发放贷款 **/
    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_LOAN_BANK_ISSUE_12)
    @Access(":issue")
    @PostMapping(value = "/{trainingId}/{id}/issue")
    public @ResponseBody
    ApiResponse issue(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        rpfLoanService.issue(id);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping({"/{trainingId}/selection"})
    @ResponseBody
    public ApiResponse<RpfLoan> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("RpfLoan.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(rpfLoanService.selection(ps));
    }

}
